/**
 *                     Remixvym (version 0.8.3)
 *             Copyright (c) 2010 by RemixLab, DISI-UNAL
 *            http://www.disi.unal.edu.co/grupos/remixlab/
 *
 * This Java package provides classes to visualize Mind Maps created in VYM
 * (View Your Mind) in a Processing Sketch
 *
 * @author Alvaro Lopez, Juan Baquero
 *
 * This source file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * A copy of the GNU General Public License is available on the World Wide Web
 * at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by
 * writing to the Free Software Foundation, 51 Franklin Street, Fifth Floor
 * Boston, MA 02110, USA
 */
package remixlab.remixvym.xml;

import java.io.UnsupportedEncodingException;

import processing.core.PApplet;
import processing.core.PImage;
import remixlab.unzipit.UnZipIt;

/**This class reads the .vym file (in XML format)*/
public class FileVymReader {

	/**Constructor. 
	 * @param Parent the PApplet (sketch) containing the map 
	 * @param Path of the .vym file (ZIP format) containing the map file in XML format*/
	public FileVymReader(PApplet parent,String file) {
		fileName=getFile(file);
		name = fileName.substring(0,fileName.length()-4);
		path = getPath(file);		
		zipHandler = new UnZipIt(file,parent);
		//esta parte es una prueba
		this.zipHandler.unpackFile(name+".xml",name+".xml");
	}

	/**Gets the content of the xml file as a String object*/
	public String getXML() {
		
		byte[] bytes = zipHandler.loadBytes(name+".xml");
		
		try {
			return new String(bytes,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	
	/**Gets the name of the map (name of file without extension)*/
	public String getName()
	{
		return name;
	}
	
	/**Gets the name of the file (including the extension)*/
	public String getFile()
	{
		return fileName;
	}
	
	/**Gets the path of the map file*/
	public String getPath()
	{
		return path;
	}
	
	/**Finds in the compressed file for speceific images*/
	public PImage getImage(String image) {
		return zipHandler.loadImage("images/"+image);
	}
	
	/**Gets the path from a URL location */
	public static String getPath(String url)
	{
		int i =0;
		for(i=url.length()-1;i>=0;i--)
		{
			if(url.charAt(i)=='/' || url.charAt(i)=='\\')
			{
				break;
			}
		}
		
		if(i==0)
		{
			return "";
		}else
		{
			return url.substring(0, i+1);
		}
	}
	
	/**Gets the filename from a URL location*/
	public static String getFile(String url)
	{
		int i =0;
		for(i=url.length()-1;i>=0;i--)
		{
			if(url.charAt(i)=='/' || url.charAt(i)=='\\')
			{
				break;
			}
		}
		
		if(i==0)
		{
			return url;
		}else
		{
			return url.substring(i+1, url.length());
		}
	}
	
	/**Name of the map*/
	String name;
	/**Name of the map file*/
	String fileName;
	/**Path of the map file*/
	String path;
	/**Reference to manage compressed files*/
	UnZipIt zipHandler;
}
